//
//  GroupNet.m
//  SportSocial
//
//  Created by yull on 15/1/16.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "GroupNet.h"

@implementation GroupNet

+ (AFHTTPRequestOperation *)addNoticeGroupWithId:(UInt64)groupId
                                 successBlock:(httpSuccessBlock)successBlock
                                    failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/follow",[self baseUrl]];
    
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId]};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)cancelNoticeGroupWithId:(UInt64)groupId
                                       successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/unFollow",[self baseUrl]];
    
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId]};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getNoticeGroupWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/listFollow",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)deleteNoticeGroupWithGroupId:(UInt64)groupId
                                            successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/unFollow",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                       @"group_id":[NSNumber numberWithLongLong:groupId]};
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getUserGroupWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/list",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)searchGroupWithName:(NSString *)groupName
                                     pageNumber:(NSInteger)page
                                       pageSize:(NSInteger)size
                                   successBlock:(httpSuccessBlock)successBlock
                                      failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/search",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"name":groupName,
                            @"page_number":[NSNumber numberWithInteger:page],
                            @"page_size":[NSNumber numberWithInteger:size]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)queryGroupWithGroupId:(UInt64)groupId successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/query",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}
+ (AFHTTPRequestOperation *)queryGroupUserWithGroupId:(UInt64)groupId
                                         successBlock:(httpSuccessBlock)successBlock
                                            failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/listGroupUser",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)inviteUser:(UInt64)userId
                               toGroup:(UInt64)groupId
                          successBlock:(httpSuccessBlock)successBlock
                             failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/invite",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"user_id":[NSNumber numberWithLongLong:userId]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)agreeInviteWithGroupId:(UInt64)groupId
                                               key:(NSString *)key
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/agreeInvite",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"key":key};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)refuseInviteWithGroupId:(UInt64)groupId
                                                key:(NSString *)key
                                       successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/refuseInvite",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"key":key};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)applyWithGroupId:(UInt64)groupId
                                successBlock:(httpSuccessBlock)successBlock
                                   failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/apply",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)agreeApplyWithGroupId:(UInt64)groupId
                                           userId:(UInt64)userId
                                              key:(NSString *)key successBlock:(httpSuccessBlock)successBlock
                                        failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/agreeApply",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"from_user_id":[NSNumber numberWithLongLong:userId],
                            @"key":key};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)refuseApplyWithGroupId:(UInt64)groupId
                                            userId:(UInt64)userId
                                               key:(NSString *)key
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/refuseApply",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"from_user_id":[NSNumber numberWithLongLong:userId],
                            @"key":key};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)kickUser:(UInt64)userId
                           fromGroup:(UInt64)groupId
                        successBlock:(httpSuccessBlock)successBlock
                           failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/kick",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":[NSNumber numberWithLongLong:groupId],
                            @"user_id":[NSNumber numberWithLongLong:userId]};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)createGroupWithName:(NSString *)name
                                        address:(NSString *)address
                                      longitude:(CGFloat)longitude
                                       latitude:(CGFloat)latitude
                                          annou:(NSString *)annou
                                   successBlock:(httpSuccessBlock)successBlock
                                      failBlock:(httpFailBlock)failBlock
{
    //data：*name 群名称；*address 地址；*longitude 经度；*latitude 纬度；annou 群公告；*reamrk 群备注
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/create",[self baseUrl]];
    NSDictionary *dataDict = @{@"name":name,
                               @"address":address,
                               @"longitude":[NSNumber numberWithFloat:longitude],
                               @"latitude":[NSNumber numberWithFloat:latitude],
                               @"remark":annou};
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *param = @{@"token":[self token],
                            @"data":dataStr};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,nil);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)updateGroupWithGroupId:(UInt64)groupId
                                              name:(NSString *)name
                                             annou:(NSString *)annou
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/update",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":@(groupId),
                            @"name":name,
                            @"annou":annou};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,nil);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadGroupIcon:(UIImage *)image
                                    groupId:(UInt64)groupId
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/uploadIcon?token=%@&group_id=%llu",[self baseUrl], [self token], groupId];
    
    NSData *iconData = UIImageJPEGRepresentation(image, 0.3f);
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:iconData name:@"photo" fileName:@"photo.jpg" mimeType:@"multipart/form-data"];
    } success:successBlock failure:failBlock];
    
    return operation;
}

+ (AFHTTPRequestOperation *)quitGroupWithGroupId:(UInt64)groupId
                                    successBlock:(httpSuccessBlock)successBlock
                                       failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/quit",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":@(groupId)};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:successBlock failure:failBlock];

    return operation;
}

+ (AFHTTPRequestOperation *)dismissGroupWithGroupId:(UInt64)groupId
                                       successBlock:(httpSuccessBlock)successBlock
                                          failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/group/breadup",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"group_id":@(groupId)};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:successBlock failure:failBlock];
    
    return operation;
}

@end
